package com.example.demo.completable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * ClassName: CompletableFutureDemo
 * author: bob.ly
 * Version: 1.0.0
 * DateTime: 2021/08/01-14:03:00
 * Description:
 */
public class CompletableFutureDemo3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("this is first task");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            int i = 1 / 0;
            return "first";
        });
        /**
         * 有异常了会抛出, 不会有输出的结果, 反正t和u会有一个为null
         */
        completableFuture.whenCompleteAsync((t, u) -> {
            System.out.println("我是:"+Thread.currentThread().getName());
            System.out.println("输出的结果:" + t);
            System.out.println(u);
        });
        System.out.println("我是:"+Thread.currentThread().getName());
        System.out.println("主线程");
        Thread.sleep(10000);

    }
}
